﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RED_NET_Core.DAL;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;

namespace RED_NET.ContentPage
{
    public partial class Photo : System.Web.UI.Page
    {
        public int VISIT_ID,Album_ID,USER_ID;
        protected void Page_Load(object sender, EventArgs e)
        {
            VISIT_ID = Int32.Parse(Request.Params["VISIT_ID"].ToString());
            Album_ID = Int32.Parse(Request.Params["Album_ID"].ToString());
            Session["Album_ID"] = Album_ID;
            //检测用户是否登录
            if (Session["USER"] == null)
            {
                Button1.Visible = Button2.Visible = Button3.Visible = false;//匿名观看
            }
            else
            {
                Users user = (Users)Session["USER"];
                USER_ID = user.UserId;
                if (VISIT_ID != USER_ID) Button1.Visible = Button2.Visible = Button3.Visible = false;//非本人观看
            }
            Button1.Attributes.Add("onClick", "return confirm('确定删除该相册？')");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Album_Photo album_photo = new Album_Photo();
            //删除对应相册里的所有照片
            try
            {
                album_photo.DeletePhotoOfAlbum(Album_ID);
                album_photo.DeleteAlbum(Album_ID);
            }
            catch (Exception ex)
            {
                //将错误信息插入数据库
                if (Session["USER"] != null)
                {
                    Users user = (Users)Session["USER"];
                    USER_ID = user.UserId;
                    DateTime time = DateTime.Now;
                    ErrorInfo_BLL.AddErrorInfo(USER_ID, ex.Message, time);
                }
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            Response.Write("<script>('删除成功！');location.href='Album.aspx?VISIT_ID=" + VISIT_ID + "';</script>");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='UpdateAlbum.aspx?VISIT_ID=" + VISIT_ID + "&Album_ID=" + Album_ID + "';</script>");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='AddPhoto.aspx?VISIT_ID=" + VISIT_ID + "&Album_ID=" + Album_ID + "';</script>");
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if(e.CommandName.Length>0 && e.CommandName=="OpenPhoto")
            {
                Response.Redirect("OverPhoto.aspx?VISIT_ID=" + VISIT_ID + "&Photo_ID=" + e.CommandArgument.ToString() + "&Album_ID=" + Album_ID);
            }
        }
    }
}
